package com.jtx.miaoah2.MQConfig;

import com.jtx.miaoah2.Common.RabbitMqConstants;
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 季台星
 * @Date 2023 07 23 20 12
 */
@Configuration
public class MqConfig {
    /**
     * 一个交换机 绑定2个队列
     * @return
     */
    @Bean
    public Exchange snappedExchange(){
        return new DirectExchange(RabbitMqConstants.SNAPPED_EXCHANGE,false,false);
    }
    @Bean
    public Queue snappedQueue(){
        return QueueBuilder.durable(RabbitMqConstants.SNAPPED_QUEUE_01).build();
    }

    @Bean
    public Queue snappedQueue1(){
        return QueueBuilder.durable(RabbitMqConstants.SNAPPED_QUEUE_02).build();
    }
    @Bean
    public Binding bindingSnappedQueue(){
        return BindingBuilder.bind(snappedQueue()).to(snappedExchange()).with(RabbitMqConstants.SNAPPED_ROUTEING_KEY).noargs();
    }

    @Bean
    public Binding bindingSnappedQueue1(){
        return BindingBuilder.bind(snappedQueue1()).to(snappedExchange()).with(RabbitMqConstants.SNAPPED_ROUTEING_KEY).noargs();
    }

    @Bean
    public Exchange orderExchange(){
        return ExchangeBuilder.directExchange(RabbitMqConstants.ORDER_EXCHANGE).durable(true)
                .build();
    }
    @Bean
    public Exchange orderDeathExchange(){
        return ExchangeBuilder.directExchange(RabbitMqConstants.ORDER_DEATH_EXCHANGE).durable(true)
                .build();
    }

    @Bean
    public Queue orderDeathQueue(){
        return QueueBuilder.durable(RabbitMqConstants.ORDER_DEATH_QUEUE)
                .build();
    }
    @Bean
    public Binding bindingDeathQueue(){
        return BindingBuilder.bind(orderDeathQueue())
                .to(orderDeathExchange()).with(RabbitMqConstants.ORDER_DEATH_KET).noargs();
    }
    @Bean
    public Queue orderQueue(){
        return QueueBuilder.durable(RabbitMqConstants.ORDER_QUEUE_01)
                .deadLetterExchange(RabbitMqConstants.ORDER_DEATH_EXCHANGE)
                .deadLetterRoutingKey(RabbitMqConstants.ORDER_DEATH_KET)
                .ttl(1000 * 60 * 5).build();
    }
    @Bean
    public Binding bindingOrderQueue(){
        return BindingBuilder.bind(orderQueue())
                .to(orderExchange()).with(RabbitMqConstants.ORDER_ROUTEING_KEY).noargs();
    }
}
